<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactStage\Logic;

use Hyperf\Di\Annotation\Inject;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkContact\Contract\WorkContactTagContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;
use MoChat\Plugin\ContactStage\Contract\ContactStageContract;
use MoChat\Plugin\ContactStage\Contract\ContactStageEmployeeContract;
use MoChat\Plugin\ContactStage\Contract\ContactStageRuleContract;

class ContactStageEmployeeUpdateLogic
{
    /**
     * 阶段管理表.
     * @Inject
     * @var ContactStageContract
     */
    protected $contactStageService;

    /**
     * 联系人表.
     * @Inject
     * @var ContactStageRuleContract
     */
    protected $contactStageRuleService;

    /**
     * 员工客户阶段关联表.
     * @Inject
     * @var ContactStageEmployeeContract
     */
    protected $ContactStageEmployeeService;

    /**
     * 客户标签表.
     * @Inject
     * @var WorkContactTagContract
     */
    protected $workContactTagService;

    /**
     * 客户-标签关联表.
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivotService;

    /**
     * 通讯录 - 客户 中间表.
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeService;

    public function handle(int $contactId, int $employeeId, int $corpId)
    {
        $followRoute = $this->contactStageRuleService->getContactStageRuleByCorpId($corpId);
        if (empty($followRoute)) {
            return;
        }

        if ($followRoute['stageAuto'] != 1) {
            return;
        }

        $stage = $this->contactStageService->getContactStagesByCorpId($corpId);
        if (empty($stage)) {
            return;
        }

        $tagId = 0;
        $status = [];
        foreach ($stage as $k => $v) {
            $v['autoAttribute'] = json_decode($v['autoAttribute'], true);
            foreach ($v['autoAttribute'] as $k1 => $v1) {
                foreach ($v1 as $k2 => $v2) {
                    if ($v2['type'] == 0) {// 客户阶段
                        $userContact = $this->ContactStageEmployeeService->getContactStageEmployeeByOtherId($contactId, $employeeId, $corpId);
                        if ($userContact['stageId'] == $v2['condition']) {
                            $status[] = 1;
                        }
                    } elseif ($v2['type'] == 1) {// 流失状态0：未流失，1：已流失
                        $contactEmployee = $this->workContactEmployeeService->getWorkContactEmployeeByCorpIdContactIdEmployeeId($contactId, $corpId, $employeeId);
                        if ($v2['condition'] == 0 && $contactEmployee['status'] == 1) {
                            $status[] = 1;
                        } elseif ($v2['condition'] == 1 && ($contactEmployee['status'] == 2 || $contactEmployee['status'] == 3)) {
                            $status[] = 1;
                        } else {
                            $status[] = 0;
                        }
                    } elseif ($v2['type'] == 2) {// 客户标签
                        $tagIds = $this->workContactTagService->getWorkContactTagsByCorpIdIds($corpId, $v2['condition'], ['id']);
                        $tag = $this->workContactTagPivotService->getWorkContactTagsPivotByContactIdEmployeeIdTagIds($contactId, $employeeId, $tagIds);
                        if (empty($tag)) {
                            $status[] = 0;
                        } else {
                            $status[] = 1;
                        }
                    }
                }
            }

            if (! in_array(0, $status)) {
                $tagId = $v['id'];
            }
        }

        $this->ContactStageEmployeeService->updateContactStageEmployeeById($tagId, ['stage_id' => $tagId]);
    }
}
